package com.yanqu.road.utils;


import java.io.InputStream;
import java.util.Properties;

/**
 * @Description
 * @Author cwq
 * @Data 2023/4/1 16:38
 */
public class CodeVersion {

    /**
     * 打印代码版本
     */
    public static void printCodeVersion() {
        String versionInfo = "";
        String lastCommitTime = "";
        Properties properties = readGitProperties();
        if (properties != null) {
            versionInfo = properties.getProperty("git.branch");
            lastCommitTime = properties.getProperty("git.commit.time");
        }
        LogUtils.COMMON.info("代码版本 code version ：{}, 最后一次提交时间：{}", versionInfo, lastCommitTime);
    }

    private static Properties readGitProperties() {
        InputStream inputStream = null;
        try {
            ClassLoader classLoader = CodeVersion.class.getClassLoader();
            inputStream = classLoader.getResourceAsStream("git.properties");
            if (inputStream != null) {
                Properties gitProperties = new Properties();
                gitProperties.load(inputStream);
                return gitProperties;
            }
        } catch (Exception e) {
            LogUtils.ERROR.error("get git version info fail", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                LogUtils.ERROR.error("close inputStream fail", e);
            }
        }
        return null;
    }

}
